package com.ruoyi.web.controller.biz;

import java.util.List;

import com.ruoyi.biz.domain.EquipmentWarnUser;
import com.ruoyi.biz.domain.EquipmentWarnUserInput;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.biz.service.IEquipmentWarnUserService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2020-12-18
 */
@RestController
@RequestMapping("/system/equipment/warn/user")
public class EquipmentWarnUserController extends BaseController
{
    @Autowired
    private IEquipmentWarnUserService equipmentWarnUserService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('operate:contact:manage:list')")
    @GetMapping("/list")
    @ApiOperation("查询设备告警联系人列表")
    public TableDataInfo list(EquipmentWarnUser equipmentWarnUser)
    {
        startPage();
        List<EquipmentWarnUser> list = equipmentWarnUserService.selectEquipmentWarnUserList(equipmentWarnUser);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     *//*
    @PreAuthorize("@ss.hasPermi('system:user:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(EquipmentWarnUser equipmentWarnUser)
    {
        List<EquipmentWarnUser> list = equipmentWarnUserService.selectEquipmentWarnUserList(equipmentWarnUser);
        ExcelUtil<EquipmentWarnUser> util = new ExcelUtil<EquipmentWarnUser>(EquipmentWarnUser.class);
        return util.exportExcel(list, "user");
    }*/

   /* *//**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('operate:contact:manage:query')")
    @GetMapping(value = "/{userId}/{equipmentId}")
    @ApiOperation("获取设备告警联系人信息")
    public AjaxResult getInfo(@PathVariable("userId") Integer userId,@PathVariable("equipmentId") Integer equipmentId)
    {
        return AjaxResult.success(equipmentWarnUserService.selectEquipmentWarnUserById(userId,equipmentId));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('operate:contact:manage:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增设备告警联系人信息")
    public AjaxResult add(@RequestBody EquipmentWarnUserInput equipmentWarnUserInput)
    {
        int result = 0;
        try {
            // 判断
            if (equipmentWarnUserInput != null) {
                String[] userIds = equipmentWarnUserInput.getSelectedContactIds();
                String[] equipmentIds = equipmentWarnUserInput.getSelectedEquipmentIds();
                // 表示要插入多少条记录
                for (int i = 0; i < userIds.length; i++) {
                    for (int j = 0; j < equipmentIds.length; j++) {
                        EquipmentWarnUser equipmentWarnUser = new EquipmentWarnUser();
                        equipmentWarnUser.setUserId(Integer.parseInt(userIds[i]));
                        equipmentWarnUser.setEquipmentId(Integer.parseInt(equipmentIds[j]));
                        result += equipmentWarnUserService.insertEquipmentWarnUser(equipmentWarnUser);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("该联系人已与该设备关联，请勿重复关联");
        }
        return toAjax(result);
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('operate:contact:manage:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改设备告警联系人信息")
    public AjaxResult edit(@RequestBody EquipmentWarnUser equipmentWarnUser)
    {
        return toAjax(equipmentWarnUserService.updateEquipmentWarnUser(equipmentWarnUser));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('operate:contact:manage:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    @ApiOperation("批量删除设备告警联系人信息")
    public AjaxResult remove(@PathVariable Integer[] userIds)
    {
        return toAjax(equipmentWarnUserService.deleteEquipmentWarnUserByIds(userIds));
    }


    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('operate:contact:manage:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/{userId}/{equipmentId}")
    @ApiOperation("删除设备告警联系人信息")
    public AjaxResult delete(@PathVariable("userId") Integer userId,@PathVariable("equipmentId") Integer equipmentId) {
        return toAjax(equipmentWarnUserService.deleteEquipmentWarnUserById(userId,equipmentId));
    }
}
